/**
 ******************************************************************************
 * @file    *.h
 * @author  pengli.wang
 * @version V3.5.0
 * @date    08-April-2018
 * @brief   Handle the radar output data .
 ******************************************************************************
 * @attention
 *
 * THE PRESENT FIRMWARE WHICH IS FOR GUIDANCE ONLY AIMS AT PROVIDING CUSTOMERS
 * WITH CODING INFORMATION REGARDING THEIR PRODUCTS IN ORDER FOR THEM TO SAVE
 * TIME. AS A RESULT, HURYS SHALL NOT BE HELD LIABLE FOR ANY
 * DIRECT, INDIRECT OR CONSEQUENTIAL DAMAGES WITH RESPECT TO ANY CLAIMS ARISING
 * FROM THE CONTENT OF SUCH FIRMWARE AND/OR THE USE MADE BY CUSTOMERS OF THE
 * CODING INFORMATION CONTAINED HEREIN IN CONNECTION WITH THEIR PRODUCTS.
 *
 * <h2><center>&copy; COPYRIGHT 2018 HURYS</center></h2>
 ******************************************************************************
 */
#ifndef _ARSx08_H
#define _ARSx08_H
#include "bsp.h"
#define Valid   1
#define Invalid 0

#define SendObj 1
#define SendTar 2

#define RANGE 250 /*检测距离*/
#define ELEVA 15  /*俯仰角  0°->0.25°->0.50° 32°   Sky->Road */

/*Radar Configuration message structure (0x200) (Bytes 5-7 and Bits in White are unused)*/
/*
If the validity bit is set to Valid/True, the
corresponding parameter will be updated in the ARS, otherwise it is ignored. If a
parameter is updated, it will also be stored in non-volatile memory so that it is
automatically set at startup on any subsequent power up.
*/
struct MsgRadarConfig {
    // Valid-->1 or Invalid-->0
    uint8_t radar_range_lengthV : 1;
    uint8_t radar_elevationV : 1;
    uint8_t radar_enablePowerReductionV : 1;
    uint8_t radar_output_typeV : 1;
    uint8_t radar_sensor_idV : 1;
    uint8_t : 3;  // unused

    uint8_t radar_range_length : 8;
    uint8_t radar_elevation : 8;
    uint8_t radar_enablePowerReduction : 1;
    uint8_t radar_output_type : 2;
    uint8_t : 5;  // unused
    uint8_t radar_sensor_id : 3;
    uint8_t : 5;  // unused

    uint8_t : 8;  // unused
    uint8_t : 8;  // unused
    uint8_t : 8;  // unused
};

//-----------------------------------------------------------------------
/*  Radar State message description (0x201) */
struct MsgRadarState {
    uint8_t : 1;  // unused
    uint8_t CurrentRadarPower : 1;
    uint8_t : 1;  // unused
    uint8_t SensTempErr : 1;
    uint8_t SensBlockage : 1;
    uint8_t SensDef : 1;
    uint8_t SupVolt_L : 1;
    uint8_t RadarPowerReduction : 1; /*Power reduction mode*/

    uint8_t SensorID : 4; /*Sensor ID*/
    uint8_t : 4;          // unused
    uint8_t RxInvalid : 2;
    uint8_t NVMreadStatus : 2;
    uint8_t NVMwriteStatus : 1;
    uint8_t : 3;                    // unused
    uint8_t currElevationCal : 8;   /*Elevation of the sensor's plate - 0.25deg*/
    uint8_t currRangeLengthCal : 8; /*Radar range length */
    uint8_t swMajorVersion : 8;
    uint8_t swMinorVersion : 8;
    uint8_t swBuildVersion : 8;
};
/*CAN1_Target_Status message description (0x600)*/
struct MsgTargetStatus {
    uint8_t NoOfTargetsNear;
    uint8_t NoOfTargetsFar;
};
/* CAN1_Obj_Status message structure (0x60A) (Byte 7 is not used)*/
typedef struct MsgObjectStatus {
    uint8_t NoOfObjects;
    uint8_t MeasCounter;
} MsgObjectStatus_t;
/**/
typedef struct ObjList {
    uint8_t Obj_ID;  // 0->63

    uint8_t Obj_Length;
    uint8_t Obj_Width;
    uint8_t Obj_ProbOfExist; /*Probability of existence calculated for an Object*/

    uint8_t Obj_DynProp;  /*Dynamic property*/
    uint8_t Obj_MessStat; /*Object measurement status*/

    float Obj_LongDispl;  // 0->204m ,res 0.1m
    float Obj_LatDispl;   //-51.9m->52m, res 0.1m

    float Obj_VerlLong;   // Relative velocity in longitudinal direction (x)  -  res 0.0625m/s ,offset -128m/s
    float Obj_VerlLat;    // Relative velocity in lateral direction (y)
    float Obj_AccelLong;  // Relative longitudinal acceleration  -res 0.0625m/s^2 ,offset -16m/s^2
} ObjList_t;
typedef struct ObjStruct {
    uint8_t Obj_ID0;         // 0->63
    uint8_t Obj_ID1;         // 0->63
    uint8_t Obj_LongDispl0;  // x
    uint8_t Obj_LongDispl1;  // x
    uint8_t Obj_LatDispl0;   // y
    uint8_t Obj_LatDispl1;   // y
    uint8_t Obj_VerlLong0;   // Relative velocity in longitudinal direction (x)  -  res 0.0625m/s ,offset -128m/s
    uint8_t Obj_VerlLong1;   // Relative velocity in longitudinal direction (x)  -  res 0.0625m/s ,offset -128m/s
    uint8_t Obj_VerlLat0;    // Relative velocity in lateral direction (y)
    uint8_t Obj_VerlLat1;    // Relative velocity in lateral direction (y)
} ObjStruct_t;

typedef struct ClusterList {
    uint8_t ID;  // 0->255
    float   DistLong;
    float   DistLat;
    float   VrelLong;
    uint8_t DynProp;
    float   VrelLat;
    uint8_t RCS;
} ClusterList_t;

enum {
    MOVING               = 0,
    STATIONARY           = 1,
    ONCOMING             = 2,
    STATIONARY_CANDIDATE = 3,
    UNKNOW               = 4,
    CROSING_STATIONARY   = 6,
    CROSING_MOVING       = 6,
    STOPPED              = 7,
};
#define WidthRIGHT         4.0   // Y坐標
#define WidthLEFT          -6.0  // Y坐標
#define DISTANCE_WARN_MODE 0     // 1:distance 0:time

#define DISTANCE_LOWSPEED_WARN 5    // 低速距离
#define WARNSPEED              0.0  // 速度
#define DISTANCE_WARN_START    150  // 闪烁 距离范围

#define DISTANCE_WARN_LONGBEEP 15  // 长亮距离

#define TIME_WARN_START    150  // 闪烁 时间范围
#define TIME_WARN_LONGBEEP 3    // 长亮时间范围
#define WARN_SPEED         0    //
#define OVER_SPEED         10   //

/***************************/
extern u16         radarChangeFlag;
extern ObjStruct_t objOncomingBuff[10];
extern u8          speedTrigger;
/***************************/
void                 ARS_Init(void);
struct MsgRadarState GetRadarState(CanRxMsg *canrxMsg);
void                 ARSObjStatusHandle(CanRxMsg *CANRxMsg);
struct ObjList       ARS_Obj_Handle(CanRxMsg *CANRxMsg);
struct ClusterList   ASR_Cluster_Handle(CanRxMsg *CANRxMsg);
void                 radarDataProcessing(void);

#endif
